package com.prs.crm.dao.cqunicom.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.cqunicom.OrderChartByMonthDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.cqunicom.OrderChartByMonth;
import com.prs.crm.domain.cqunicom.PoplarOrder;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2011-3-29 下午04:00:01
 * 简单说明
 */

public class OrderChartByMonthDaoImpl extends BaseDaoImpl<OrderChartByMonth>
		implements OrderChartByMonthDao {

	@SuppressWarnings("unchecked")
	@Override
	public OrderChartByMonth getByTimeSerNames(final String times,
			final String serviceName,final String service) {
		 return (OrderChartByMonth) this.getHibernateTemplate().execute(
					new HibernateCallback()
					{
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(OrderChartByMonth.class);
				        	String dates[]=times.split("-");
				        	String overtimes="";
				        	int month=Integer.parseInt(dates[1]);
				        	if(month==1){
				        		overtimes=(Integer.parseInt(dates[0])-1)+"-12"+"-"+dates[2];
				        	}else{
				        		String m=((month-1)+"").trim();
				        		if(m.length()==1){
				        			m="0"+m;
				        		}
				        		overtimes=dates[0]+"-"+m+"-"+dates[2];
				        	
				        	}
				        	criteria.add(Restrictions.eq("monDate",overtimes));		
				        	criteria.add(Restrictions.eq("monHy",serviceName));	
				        	criteria.add(Restrictions.eq("monWord",service));
				        	if(criteria.list().size()==0){
				        		return null;
				        	}else{
				        		return criteria.list().get(0);
				        	} 	
				        }
					}
			);
	}

}
